package com.fingard.dsp.bank.directbank.bjums01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ICloseOrder;
import com.fingard.dsp.bank.directbank.cmbunion01.util.BoingPay;
import com.fingard.dsp.bank.reqrespobj.closeorder.CloseOrderState;
import com.fingard.dsp.bank.reqrespobj.closeorder.ReqCloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.RetAtsCloseOrder;
import com.fingard.text.StringHelper;

import java.util.Map;

public class BJUMS01CloseOrder extends BJUMS01UnifiedOrder implements ICloseOrder {
    @Override
    public RetAtsCloseOrder closeOrder(ReqCloseOrder p_req) {
        RetAtsCloseOrder retAts = new RetAtsCloseOrder(p_req.reqHeader);
        retAts.rdSeq = p_req.rdSeq;
        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(
                    p_req.ownAct, "");
            if (actItem != null) {
                Map<String, Object> param = getParam(actItem);
                param.put("tranno",  BJUMS01TransType.QR001007);
                param.put("reqOrderNo",  getQryTransID());
                param.put("oriOrderNo",  p_req.rdSeq);
                String clientIp = actItem.createIP;
                if (StringHelper.hasAnyChar(clientIp)) {
                    param.put("clientIp", clientIp);//后台通知地
                }
                param.put("merchId",  actItem.merchantId);
                String sign = addSign(null, param, actItem.saltKey);
                WriteBankLogLn("签名结果:"+sign);
                param.put("signature", sign);
                JSONObject jsonObj=new JSONObject(param);
                String[] tmpStrRet = sendToBank(jsonObj.toJSONString(),bankFront.serverURL);
                if(tmpStrRet[0].length() == 0){
                    JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                    String  pay_result = resultObj.getString("respCode");
                    String resp_msg = resultObj.getString("respMsg");
                    if("000000".equals(pay_result)){
                        retAts.closeState = CloseOrderState.Close_Success;
                    }else {
                        retAts.closeState = CloseOrderState.Close_Fail;
                    }
                    retAts.rdSeq = p_req.rdSeq;
                    retAts.respCode = "0";
                    retAts.respInfo = "";
                }else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号:[" + p_req.ownAct + "]相关商户配置信息!";
            }
        } catch (Exception ex) {
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            ex.printStackTrace();
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
